import random
import time

import pygame

import setting

# 标准化
name_color = setting.color_Firebrick  # 名字颜色
attribute_color = setting.color_DeepSkyBlue1  # 属性颜色
text_color = setting.color_LimeGreen  # 文本颜色
dev = setting.offset  # 单元格偏移量
uniW = setting.unit_W  # 单元格宽
uniH = setting.unit_H  # 单元格高

# 创建窗口
window = pygame.display.set_mode(setting.windowSize)  # 创建窗口(windowSize)
pygame.display.set_caption(setting.windowName)  # 窗口名称(windowName)
pygame.display.set_icon(setting.windowIcon)  # 窗口图标(windowIcon)
window.fill(setting.color_Black)  # 窗口背景色(color)

# 位置信息
# 地图坐标
mapXYValue = [
    (0, 0), (uniW, 0), (uniW * 2, 0), (uniW * 3, 0), (uniW * 4, 0), (uniW * 5, 0), (uniW * 5, uniH),
    (uniW * 5, uniH * 2), (uniW * 5, uniH * 3), (uniW * 4, uniH * 3), (uniW * 3, uniH * 3), (uniW * 2, uniH * 3),
    (uniW, uniH * 3), (0, uniH * 3), (0, uniH * 2), (0, uniH * 1)]
# 实体化建筑信息坐标
mapNameXYValue = []
mapPriceXYValue = []
mapTaxValue = []
mapImageValue = []
mapChessXYValue = []
mapInformationXYValue = (uniW + dev, uniH * 2 + dev)
# 建筑位置信息初始化
for i in range(16):
    h1 = setting.text_H_01  # 01字高
    h2 = setting.text_H_02  # 02字高
    mapNameXYValue.append((mapXYValue[i][0] + dev, mapXYValue[i][1] + dev))
    mapPriceXYValue.append((mapXYValue[i][0] + dev, mapXYValue[i][1] + dev + h1 + 2))
    mapTaxValue.append((mapXYValue[i][0] + dev, mapXYValue[i][1] + dev + h1 + h2 + 4))
    mapImageValue.append((mapXYValue[i][0] + dev, mapXYValue[i][1] + dev + h1 + h2 * 2 + 6))
    mapChessXYValue.append(
        (mapXYValue[i][0] + uniW - dev - setting.chessImage_W, mapXYValue[i][1] + uniH - dev - setting.chessImage_H))

    # 建筑信息(标识，地图坐标，房屋数，价格，租金，拥有者，名字)


class Building:
    mark = None
    name = None
    location = []
    house = 0
    price = 0
    tax = 0
    owner = None
    image = None
    imageLocation = []

    def __init__(self, mark, name, price, tax, location, image):  # 建筑数据初始化
        self.mark = mark
        self.name = name  # 建筑名字
        self.house = 0  # 房屋数
        self.price = price  # 建筑价格
        self.tax = tax  # 建筑租金
        self.location = location
        self.image = image


# 实例化建筑
begin = Building(0, '开始', 0, 0, 0, setting.buildImage_00)
b1 = Building(0, '01', 1000, 100, 1, setting.buildImage_01)
b2 = Building(0, '02', 1000, 100, 2, setting.buildImage_02)
b3 = Building(0, '03', 1000, 100, 3, setting.buildImage_03)
b4 = Building(0, '04', 1000, 100, 4, setting.buildImage_04)
b5 = Building(0, '05', 1000, 100, 5, setting.buildImage_05)
b6 = Building(0, '06', 1000, 100, 6, setting.buildImage_06)
b7 = Building(0, '07', 1000, 100, 7, setting.buildImage_07)
b8 = Building(0, '08', 1000, 100, 8, setting.buildImage_08)
b9 = Building(0, '09', 1000, 100, 9, setting.buildImage_09)
b10 = Building(0, '10', 1000, 100, 10, setting.buildImage_10)
b11 = Building(0, '11', 1000, 100, 11, setting.buildImage_11)
b12 = Building(0, '12', 1000, 100, 12, setting.buildImage_12)
b13 = Building(0, '13', 1000, 100, 13, setting.buildImage_13)
b14 = Building(0, '14', 1000, 100, 14, setting.buildImage_14)
b15 = Building(0, '15', 1000, 100, 15, setting.buildImage_15)

# 建筑组
buildings = [begin, b1, b2, b3, b4, b5, b6, b7, b8, b9, b10, b11, b12, b13, b14, b15]


# Chess(标识,移动量,位置,位置,金币,,,名字)
class Chess:
    mark = None
    name = None
    location = []
    money = 0
    assets = []
    image = None

    def __init__(self, mark, name, location, money, image):
        self.mark = mark
        self.name = name
        self.location = location
        self.money = money
        self.image = image

    def roll_move(self):
        flag = random.randint(0, 5)  # 获取骰子点数
        for p in range(len(setting.dice)):
            window.blit(setting.dice[random.randint(0, 5)], (setting.dice_x0, setting.dice_y0))  # 骰子滚动
            pygame.display.update()  # 窗口刷新
            time.sleep(0.1)  # 延时
        window.blit(setting.dice[flag], (setting.dice_x0, setting.dice_y0))  # 骰子刷新
        text = ('您获得了 %d 点' % flag)  # 文本内容
        information(text)
        self.location += flag  # 棋子位置更新
        if self.location > 16:  # 判断棋子是否通过起点
            self.location -= 16  # 棋子位置重置
            self.money += passMoney  # 获得”由此去“奖励
        # 棋子移动
        window.blit(self.image, (  # 棋子图片刷新
            mapChessXYValue[self.location][0] - self.mark * setting.chessImage_W, mapChessXYValue[self.location][1]))
        pygame.display.update()  # 窗口刷新

    def event(self):
        if buildings[self.location].owner is None:  # 建筑是否被售卖
            if self.money >= buildings[self.location].price:  # 是否有钱购买建筑
                text = ('您是否使用 %d 来购买此地？（Y/N）' % buildings[self.location].price)
                information(text)
                key = pygame.key.get_pressed()


# 实例化棋子
chess_0 = Chess(0, 'green', 0, 20000, setting.chess_0)
chess_1 = Chess(1, 'red', 0, 20000, setting.chess_1)

# 由此去奖励
passMoney = 2000


# 信息框(标识,位置)
def information(text):  # 文本输出
    text = setting.font_02.render(text, True, text_color)  # 文本图片
    window.blit(text, (mapInformationXYValue[0], mapInformationXYValue[1]))  # 文本刷新
    pygame.display.update()  # 窗口刷新
    time.sleep(0.8)  # 延时


# 获取按键
def judge_key():
    text = '您的选择是：'  # 文本内容
    text = setting.font_02.render(text, True, text_color)  # 文本图片
    window.blit(text, (mapInformationXYValue[0], mapInformationXYValue[1] + setting.text_H_02 + 2))  # 文本刷新
    pygame.display.update()  # 窗口刷新
    key = pygame.key.get_pressed()  # 获取按键信息
    if key[pygame.K_y] or key[pygame.K_n]:  # 是否正确按键
        return key[pygame.K_y]
    else:
        judge_key()


message = {'mark': 0, 'place': (uniW, (uniH * 2))}


# Functions
# 地图刷新
def map_refresh():
    setting.window.blit(setting.backgroud, (0, 0))
    for i in range(16):
        h_1 = setting.text_h_01
        h_2 = setting.text_h_02
        line_01 = building[i]['name']  # 建筑名称
        line_02 = '地价：' + str(building[i]['price'])  # 建筑地价
        line_03 = '路费：' + str(building[i]['tax'])  # 建筑租金
        building_name = setting.font_01.render(line_01, True, name_color)
        building_price = setting.font_02.render(line_02, True, attribute_color)
        building_tax = setting.font_02.render(line_03, True, attribute_color)
        setting.window.blit(building_name, building[i]['place'])
        setting.window.blit(building_price, (building[i]['place'][0], (building[i]['place'][1] + h_1)))
        setting.window.blit(building_tax, (building[i]['place'][0], (building[i]['place'][1] + h_1 + h_2)))
        setting.window.blit(building_image[i]['image'], building_image[i]['place'])
    pygame.display.update()


# 棋子刷新
def chess_refresh():
    setting.window.blit(setting.chess_1, chess[0]['place'])
    setting.window.blit(setting.chess_2, chess[1]['place'])
    pygame.display.update()


# 操作界面刷新
def information_refresh():
    pass


# 全刷新
def refresh():
    map_refresh()
    chess_refresh()


# 棋子移动
def chess_move(chess_i, roll_point):
    map_refresh()
    chess[chess_i]['position'] += roll_point
    if chess[chess_i]['position'] >= 16:
        chess[chess_i]['position'] -= 16
        chess[chess_i]['money'] += add_money
    print(chess[chess_i]['money'])
    chess[chess_i]['place'] = (building[chess[chess_i]['position']]['place'][0] + chess[chess_i]['dev'][0],
                               building[chess[chess_i]['position']]['place'][1] + chess[chess_i]['dev'][1])
    chess_refresh()
    pygame.display.update()
    time.sleep(0.4)


# 选择判定
# def yon(event_type):
#     yes = setting.font_02.render('Yes', True, setting.red)
#     no = setting.font_02.render('No', True, setting.red)
#     setting.window.blit(yes, (uniW * 4, uniH * 2))
#     setting.window.blit(no, (uniW * 4, (uniH * 2 + setting.text_h_02)))
#     pygame.display.update()
#     i = 0
#     while i == 0:
#         text = ('您是否使用%d元购买%s' % (building[building_i]['price'], building[building_i]['name']))
#         text_c = setting.font_02.render(text, True, setting.red)
#         setting.window.blit(text_c, message['place'])
#         pygame.display.update()
#         i = input('您的选择：Y(Yes) or N(No):')
#         if event_type.pos ==
#             return True
#         elif i == 'N' or i == 'n':
#             return False
#         else:
#             print('请确认你的选择，Y or N ')
#             i = 0


# 事件判定
def event(chess_i, building_i):
    if building[building_i]['owner'] is None and chess[chess_i]['money'] >= building[building_i]['price']:
        text = ('您是否使用%d元购买%s' % (building[building_i]['price'], building[building_i]['name']))
        text_c = setting.font_02.render(text, True, text_color)
        setting.window.blit(text_c, message['place'])
        pygame.display.update()

    elif building[building_i]['owner'] is None and chess[chess_i]['money'] < building[building_i]['price']:
        text = '您的金钱不足购买此地'
        print(text)
    elif building[building_i]['owner'] == chess[chess_i]['mark'] and building[building_i]['owner'] != 'x':
        text = ('您是否使用%d元加盖旅店' % building[building_i]['price'])
        print(text)
    elif building[building_i]['owner'] != chess[chess_i]['mark'] and building[building_i]['owner'] != 'x':
        text = '您支付了xxx元路费'
        print(text)
    else:
        text = '这里是开始'
        print(text)


# 购买事件
def buy_building():
    print('您是否使用%d购买%s' % (building[1]['price'], building[1]['name']))

# 按钮显示
# def button(x, y, text):
#     base_colour = setting.black
#     base_text_colour = setting.white
#     super_colour = setting.white
#     super_text_colour = setting.black

# token 令牌
# 登录 -> 成功 -> 颁发令牌
# 攻击 -> 10(令牌) -> 后台判断 -> 令牌合法且没过期 -> 攻击成功
#
# web开发
#
# 前台（客户端）
# vue-admin 后台管理脚手架 快速开始
#
# vue 框架  +  elementUI ui框架
#
# html        骨架
# css         样式
# javascript  功能
#
# 后台（服务端）
# java 语言
# spring 框架
# spring boot  快速启动框架
#
#
# 数据库
# mysql 存数据
#
# window / macOs
# 客户端 开发
#
# 大前端 前端可以直接用数据库
#
# 开发 前后端分离 前后端耦合
